@extends("admin.frame_layout")
<?php $top_selected = "碎片管理";$page_menu = $menu_selected->title ?>
@section('content')
    <div class="formbody">
        <div class="formtitle"><span>碎片编辑</span></div>

        <form action="{{route('admin.slug.save',['id'=>isset($slug)?($slug->id):(0)])}}"
              method="post" id="news">
            {!! csrf_field() !!}

            <ul class="forminfo">

                {!! inputText("碎片标题<b> *</b>","title","text",
                    isset($slug)?$slug->title:"",
                    "用来描述碎片用途，方便查找！") !!}

                {!! inputText("碎片标识<b> *</b>","slug","text",
                    isset($slug)?$slug->slug:"",
                    "标识建议最长不超过20个英文字符！") !!}

                <li>
                    <label class="lable">碎片分类<b> *</b></label>
                    <select name="cid" id="cid">
                        <option value="">请必须选择碎片分类</option>
                        @foreach($classifies as $i)
                            <option value="{{$i->id}}" {{isset($slug)?($slug->cid==$i->id?"selected":""):""}} {{isset($menu_selected)?($menu_selected->id==$i->id?"selected":""):""}}>{{$i->title}}</option>
                        @endforeach
                    </select>
                    <i for="cid">请选择碎片分类</i>
                </li>

                {!! inputText("碎片图标","icon","text",
                    isset($slug)?$slug->icon:"",
                    "请填写需要用到的图标类名！") !!}

                <li>
                    <label class="lable">碎片配图</label>
                    <input type="file" name="img" id="img" class="dfinput">
                    <i for="img">请选择碎片配图</i>
                </li>
                @if(isset($slug))
                    <li>
                        <label class="lable">&nbsp;</label>
                        <img src="{{asset($slug->img)}}" height="90">
                    </li>
                @endif

                <li>
                    <label class="lable">碎片内容<b> *</b></label>
                    {{--加载编辑器的容器--}}
                    <div style="float: left;width: 800px;">
                        <script name="content" id="content"
                                type="text/plain">{!! $slug->content or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>

                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="确认保存"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    {{-- 实例化编辑器 --}}
    <script type="text/javascript">
        var ue = UE.getEditor('content', {
            initialFrameHeight: 250
        });

        $("#news").validate({
            rules: {
                title: "required",
                slug: "required",
                cid: "required",
                content: "required"
            },
            errorPlacement: function (error, element) {
                $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
            },
            submitHandler: function (form) {
                if (ue.hasContents()) {
                    ajaxSub(form);
                } else {
                    toastr["error"]("请填写文章详情！");
                }
            }
        });

        /**
         * ajax提交请求
         */
        function ajaxSub(form) {
            $(form).ajaxSubmit({
                dataType: "json",
                type: "post",
                success: function showSuccess(data) {
                    if (data.code == 0) {
                        toastr["success"](data.msg);
                        setInterval(function () {
                            window.location = "{{route('admin.slug',['cid'=>$menu_selected->id])}}";
                        }, 2000);
                    } else {
                        toastr["error"](data.msg);
                    }
                }
            });
        }
    </script>
@endsection
